package com.test.service.impl;

import com.test.dao.IHeatBaseDao;
import com.test.dao.IHouseDao;
import com.test.dao.IHouseMeterCurrentDao;
import com.test.dao.IHouseValveCurrentDao;
import com.test.model.*;
import com.test.service.*;
import com.test.utils.HouseMeterCurrentGenerate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by ZZF on 2017/7/17.
 */
@Service
@Transactional
public class HouseServiceImpl implements IHouseService {

    @Autowired
    IHouseDao houseDao;
    @Autowired
    IBuildValueCurrentService buildValueCurrentService;
    @Autowired
    IHeatBaseDao heatBaseDao;
    @Autowired
    IHouseValveCurrentDao houseValveCurrentDao;
    @Autowired
    IHouseMeterCurrentDao houseMeterCurrentDao;
    @Autowired
    IHouseValueCurrentService houseValueCurrentService;

    @Autowired
    IHouseMeterHistoricalService houseMeterHistoricalService;
    @Autowired
    IHouseValueHistoricalService houseValueHistoricalService;

    @Override
    public void simulationHouse(List<House> houseList, boolean buildFlag) {

        // List<House> houseAll = houseDao.getAll(House.class);
        if (!houseList.isEmpty()) {
            //随机户阀的开关状态
            houseValueCurrentService.updateSwitchState();

            for (House house : houseList) {
                HouseValve houseValve = houseDao.findHouseValve(house.getUniqueId());
                HouseMeter houseMeter = houseDao.findHouseMeter(house.getUniqueId());

                String houseFlag = null;

                //如果户的表和阀都没有进行下一个
                if (houseValve == null && houseMeter == null) {
                    continue;
                }
                //要是有阀的数据，就根据阀的开关状态 判断户表的数据
                if (houseValve != null) {
                    HouseValveCurrentData valveCurrentData = houseValveCurrentDao.get(HouseValveCurrentData.class,
                            houseValve.getUniqueId());
                    Boolean switchState = valveCurrentData.getSwitchState();

                    if (!switchState) {
                        houseFlag = "0";
                    }

                    //添加户阀的历史记录
                    houseValueHistoricalService.addHistorical(valveCurrentData);
                }

                //最后判断下楼阀是否是开的，不然户表的值为0
                if (!buildFlag) {
                    houseFlag = "0";
                }

                //户要有表才能更改表的数据，才能添加历史记录
                if (houseMeter != null) {
                    HouseMeterCurrentData meterCurrentData = houseMeterCurrentDao.get(HouseMeterCurrentData.class, houseMeter.getUniqueId());

                    //让户表数据从新生成
                    HouseMeterCurrentData generate = HouseMeterCurrentGenerate.generate(meterCurrentData, houseFlag);


                    HeatBase houseBase = heatBaseDao.findHouseBase(generate.gethouseMeterId());

                    //把时间传进去，


                    //底数数据
                    BigDecimal oldFlow = houseBase.getBaseFlow();
                    BigDecimal oldHeat = houseBase.getBaseHeat();

                    //新的数据
                    BigDecimal newFlow = generate.getAccFlow();
                    BigDecimal newHeat = generate.getAccHeat();

                    //给底数对象添加结束时间和结束参数
                    houseBase.setEndDate(generate.getPlanTime());
                    houseBase.setEndFlow(newFlow);
                    houseBase.setEndHeat(newFlow);

                    //季使用量
                    BigDecimal baseFlow = newFlow.subtract(oldFlow);
                    BigDecimal baseHeat = newHeat.subtract(oldHeat);

                    //给底数对象赋值季使用量
                    houseBase.setSeasonHeat(baseHeat);
                    houseBase.setSeasonFlow(baseFlow);

                    //修改底数对象
                    heatBaseDao.update(houseBase);



                    //给户表实时对象添加季使用量
                    generate.setSeasonHeat(baseHeat);
                    generate.setSeasonFlow(baseFlow);

                    // 改变户表的实时数据
                    houseMeterCurrentDao.update(generate);


                    //添加户表的历史记录
                    houseMeterHistoricalService.addHistorical(generate);


                }


            }
        }


    }



}
